//
//  HouseCarRentCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/9/15.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SDWebImage

class HouseCarRentCell: BaseTableViewCell {
    @IBOutlet weak var leftImageView: UIImageView!

    @IBOutlet weak var lastMargin: NSLayoutConstraint!
    @IBOutlet weak var infoLabel: UILabel!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var bgViewR: NSLayoutConstraint!
    @IBOutlet weak var bgViewH: NSLayoutConstraint!
    @IBOutlet weak var bgView: UIView!
    
    @IBOutlet weak var phoneBtn: UIButton!
    @IBOutlet weak var bottomMargin: NSLayoutConstraint!
    
    
    var houseCarRentColletionView : ImageBrowser!
    
    
    var model : BusinessActivityModel?{
        
        didSet{
            
            if model == nil {
                return
            }
            
            
            
            infoLabel.attributedText = attributedString(str: "出租:东津世纪城12区花漫里13号楼,127平，三卧一庁,冰箱,洗衣机,空调,床,电视,沙发,餐桌,灶具,衣柜齐备.联系人:陈女士15172614596", font: UIFont.systemFont(ofSize: 15))
            
            if model!.range == nil{
                
                model!.phoneNumber = nil
                
            }else{
            
            let tempStr = "出租:东津世纪城12区花漫里13号楼,127平，三卧一庁,冰箱,洗衣机,空调,床,电视,沙发,餐桌,灶具,衣柜齐备.联系人:陈女士15172614596" as! NSMutableString
                
             model!.phoneNumber = tempStr.substring(with: model!.range!)
                
                QL1(model!.phoneNumber)
            
            }
            
            
            if model!.phoneNumber == nil{
                
                phoneBtn.isHidden = true
                
            }else{
                
                phoneBtn.isHidden = false

                
            }
            
            let url = URL(string: model!.article_cover)
            
            if url == nil {
                
                bgView.isHidden = true
                
                bgViewH.constant = 0
                
                houseCarRentColletionView.thumPicURLs = [URL]()
                
                lastMargin.constant = 0
                
                bottomMargin.constant = 12
                
                return
            }
            
            
            
            lastMargin.constant = 10
            
            bottomMargin.constant = 22
            
            
            let layout = houseCarRentColletionView.collectionViewLayout as! ImageBrowserFlowLayout
            
            let (colleSize,itemSize) = layout.calculateSize(thumPicURLs: [url!], leftRightMargin: 72)
            
            bgViewH.constant = colleSize.height
            
            bgViewR.constant = KScreenW - colleSize.width - 64
            
            
            houseCarRentColletionView.frame = CGRect(x: 0, y: 0, width: colleSize.width, height: colleSize.height)
            
            
            // 8.3设置cell尺寸
            
            if itemSize !=  CGSize(width: 0, height: 0) {
                
                layout.itemSize = itemSize
                
            }
            
            houseCarRentColletionView.thumPicURLs = [url!]
            
            
            houseCarRentColletionView.bigPicURLs = nil
            
            
            houseCarRentColletionView.reloadData()
            
            
        }
        
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        contentView.backgroundColor = UIColor.white
        
        timeLabel.textColor = UIColor.dakGray()
        
        
//        nameLabel.textColor = UIColor.magenta
        
        leftImageView.addCornerAndBorder(borderColor: nil, borderWidth: 0, masksToBounds: true, cornerRadius: 22)
        
        let temp = ImageBrowserFlowLayout()
        
        houseCarRentColletionView = ImageBrowser.init(frame: CGRect(x: 0, y: 0, width: KScreenW - 72, height: 20), collectionViewLayout: temp)
        
        houseCarRentColletionView.backgroundColor = UIColor.viewBgGrey()
        
        bgView.addSubview(houseCarRentColletionView)
        
        

        
    }
    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)


    
    }
    
    
    @IBAction func phoneBtnDidTap(_ sender: UIButton) {
        
        
        
        let telStr = "telprompt://\(model!.phoneNumber!)"
        
        let url = URL(string: telStr)
        
        if url == nil {
            return
        }else{
            
            if  UIApplication.shared.canOpenURL(url!){
                
                UIApplication.shared.openURL(url!)
                
            }
            
        }
        

        
        
    }
    
    
    func attributedString(str: String, font: UIFont) -> NSAttributedString? {
        
        let pattern = "[1][0-9]{10}"
        
        //创建正则表达式对象
        //caseInsensitive  忽略大小写  最常用
        //dotMatchesLineSeparators 允许 . 匹配任意字符，包括回车换行
        let regex = try! NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
        
        //根据规则匹配指定字符串中第一个符合规则的字符串，并且返回一个NSTextCheckingResult
        let res1 = regex.matches(in: str, options: NSRegularExpression.MatchingOptions.init(rawValue: 0), range: NSRange(location: 0, length: str.characters.count))
        
        // 根据原有字符串生成属性字符串
        let strM = NSMutableAttributedString(string: str)
        
        strM.addAttribute(NSFontAttributeName, value: font, range:NSMakeRange(0, str.characters.count))
        
        strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range:NSMakeRange(0, str.characters.count) )
        
        model!.range = nil
    
        
        for temp in res1{
            
            
            strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range:temp.range )
            
            model!.range = temp.range
            
        }
        
        
        return strM
        
        
        
    }
    
    
    
}





